app.controller('ordersCtrl',['$scope','$modal','ordersService','utilService','toaster','$filter', function($scope,$modal,ordersService,utilService,toaster,$filter){
    $scope.convert=function(){
        $scope.orders.buyStartDate = $filter("date")($scope.orders.buyStartDate, "yyyy-MM-dd");
        $scope.orders.buyEndDate = $filter("date")($scope.orders.buyEndDate, "yyyy-MM-dd");
        $scope.orders.startDate = $filter("date")($scope.orders.startDate, "yyyy-MM-dd");
        $scope.orders.endDate = $filter("date")($scope.orders.endDate, "yyyy-MM-dd");
        $scope.orders.useStartDate = $filter("date")($scope.orders.useStartDate, "yyyy-MM-dd");
        $scope.orders.useEndDate = $filter("date")($scope.orders.useEndDate, "yyyy-MM-dd");
    };
    $scope.page={};
    $scope.page.currentPage=1;
    //分页
    $scope.replacePage = function () {
        $scope.currentPage= $scope.page.currentPage;
        $scope.pageChanged();
    };

    // call server.
    $scope.orders = {
        status:-1,
        type:2
    };
    $scope.query = function(){
        $scope.convert();
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'orders',
            params:$scope.orders
        }).then(function(result){
            console.log(result);
        });
    }
    $scope.query();

    //更改订单状态
    $scope.updateStatus = function(id, status){
        var param = {
            id:id,
            status:status
        };
        if(status == 7){
            openModal("您确定要取消该订单吗？");
        }else if(status == 8){
            openModal("您确定要退货吗？");
        }else if(status == 5){
            openModal("您确定已使用了吗？");
        }else if(status == 9){
            openModal("您确定退款吗？");
        }else if(status == 1 || status == 4){
            openModal("您确定已付款了吗？");
        }else if(status == 3){
            openModal("您确定状态改为确认收货吗？");
        } else if (status == 2) {
            openModal("您确定已配送了吗？");
        } else if (status == 10) {
            openModal("您确定接受该订单吗？");
        } else if (status == 6) {
            openModal("您确定交易完成了吗？");
        }


        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"修改状态",
                controller:"updateStatusCtrl",
                message:msg,
                data:param
            });
        }
    }

    //物流追踪
    $scope.logisticsTrace = function (logisticsNumber) {
        if(logisticsNumber.length>0){
            var modalInstance = $modal.open({
                templateUrl: 'tpl/orders/logistics_trace.html',
                controller: 'logisticsTraceCtrl',
                size:'sm',
                scope: $scope,
                resolve: {
                    data: function () {
                        var data = {};
                        data.logisticsNumber = logisticsNumber;
                        return data;
                    },
                    deps: ['$ocLazyLoad',
                        function ($ocLazyLoad) {
                            return $ocLazyLoad.load([
                                'css/content-css/logistics/logistics_trace.css'
                            ]);
                        }
                    ]
                }
            });
        }
    }


    //打开发货框
    $scope.delivery = function (id,status) {
        var data = {
            id:id,
            status:status
        };
        var modalInstance = $modal.open({
            templateUrl: 'tpl/orders/delivery_modal.html',
            controller: 'deliveryCtrl',
            size: 'sm',
            scope:$scope,
            resolve: {
                items: function () {
                    return  data;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });
    };

    //批量选择
    $scope.selected=[];

    $scope.isChecked = function(id){
        return $scope.selected.indexOf(id) >= 0 ;
    } ;

    $scope.updateSelection = function($event,id){

        var checkbox = $event.target ;
        var checked = checkbox.checked ;

        if(checked){
            $scope.selected.push(id) ;
        }else{
            var idx = $scope.selected.indexOf(id) ;
            $scope.selected.splice(idx,1) ;
        }
    };

    $scope.isCheckedAll=function(){

        if($scope.list==null){
            return;
        }

        return $scope.selected.length==$scope.list.length;
    }

    $scope.checkAll=function($event){
        var checkbox = $event.target ;
        var checked = checkbox.checked ;

        for(var i=0;i<$scope.list.length;i++){
            if(checked){
                $scope.selected.push($scope.list[i]);
            }else{
                $scope.selected.splice(0,$scope.selected.length);
            }
        }
    }

    $scope.initSelect=function(){
        $scope.selected=[];
    }


    //核销
    $scope.verifyCode=function(id,state){
        var modalInstance = $modal.open({
            templateUrl: 'tpl/orders/orders_code.html',
            controller: 'ordersCodeCtrl',
            size: 'sm',
            scope:$scope,
            resolve: {
                data:function(){
                    var ordersCode={};
                    ordersCode.id=id;
                    ordersCode.status=state;
                    return ordersCode;
                }
            }
        });
    }


    //$scope.checkShop=function(){
    //    ordersService.checkShop($scope).then(function(result){
    //        switch (result.status){
    //            case 200:
    //                $scope.shop = result.data;
    //                break;
    //            default :
    //                toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
    //                break;
    //        }
    //
    //    });
    //};
    //$scope.checkShop();

    //获得店铺列表
    $scope.shopSelectList=[];
    $scope.getShopList = function(){
        $scope.orders.shopId="";
        ordersService.getShopList($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.shopSelectList = result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }
    $scope.getShopList();

    $scope.changeState=function(state,type){
        $scope.list=new Array();
        $scope.orders.status=state;
        $scope.orders.type = type;
        $scope.query();
    };

    $scope.clearState=function(type){
        $scope.list=new Array();
        $scope.orders.status=-1;
        $scope.orders.type = type;
        $scope.query();
    };
    $scope.changeStatus = function (status) {
        $scope.list = new Array();
        $scope.orders={};
        $scope.orders.status = -1;
        $scope.orders.type = status;
        $scope.query();
        $scope.getShopList();
    };

    //导出订单日期控件开始限制
    $scope.startLimit=function(){
        $scope.end=new Date($scope.ordersdetails.end).getTime();
        $scope.start=$scope.ordersdetails.start.getTime();
        $scope.maxLimit=$scope.end;
        $scope.minLimit=$scope.maxLimit-24*60*60*1000*90;

        if($scope.ordersdetails.start!=null&&$scope.ordersdetails.start!=''){
            if($scope.start<$scope.minLimit){
                $scope.ordersdetails.start=$scope.minLimit;
            }
            $scope.ordersdetails.start=$filter('date')($scope.ordersdetails.start,'yyyy-MM-dd');
        }
    }
    //导出订单日期控件结束限制
    $scope.endLimit=function(){
        $scope.end=$scope.ordersdetails.end.getTime();
        $scope.start=new Date($scope.ordersdetails.start).getTime();
        $scope.maxLimit=new Date().getTime();
        $scope.minLimit=$scope.maxLimit-24*60*60*1000*90;

        if($scope.ordersdetails.end!=null&&$scope.ordersdetails.end){
            if($scope.end<$scope.start){
                $scope.ordersdetails.start=$scope.ordersdetails.end.getTime()-24*60*60*1000*90;
            }
            $scope.ordersdetails.end=$filter('date')($scope.ordersdetails.end,'yyyy-MM-dd');
            $scope.ordersdetails.start=$filter('date')($scope.ordersdetails.start,'yyyy-MM-dd');
        }
    }
    //导出订单及订单明细模态框
    $scope.exportData = function (type) {
        $scope.ordersdetails={};
        $scope.ordersdetails.end=$filter('date')(new Date(),'yyyy-MM-dd');
        $scope.ordersdetails.start=$filter('date')(new Date()-24*60*60*1000*90,'yyyy-MM-dd');
        $scope.ordersdetails.type=type;
        if (type == 2) {
            $scope.ordersStatusObj = [
                {status: '', value: "全部订单"},
                {status: 0, value: "未付款"},
                {status: 1, value: "已付款"},
                {status: 2, value: "已发货"},
                {status: 3, value: "确认收货/待评价"},
                {status: 6, value: "交易完成"},
                {status: 7, value: "交易取消"},
                {status: 8, value: "退货/退款中"},
                {status: 9, value: "退款/退货完成"}
            ]
        } else if(type == 0){
            $scope.ordersStatusObj = [
                {status: '', value: "全部订单"},
                {status: 4, value: "待使用"},
                {status: 5, value: "已使用"},
                {status: 6, value: "交易完成"},
                {status: 8, value: "退款中"},
                {status: 9, value: "退款完成"}
            ]
        } else if(type == 1){
            $scope.ordersStatusObj = [
                {status: '', value: "全部订单"},
                {status: 0, value: "未付款"},
                {status: 1, value: "已付款"},
                {status: 2, value: "已配送"},
                {status: 6, value: "交易完成"},
                {status: 7, value: "交易取消"},
                {status: 8, value: "退款中"},
                {status: 9, value: "退款完成"}
            ]
        } else if(type == 3){
            $scope.ordersStatusObj = [
                {status: '', value: "全部订单"},
                {status: 0, value: "未付款"},
                {status: 1, value: "已付款"},
                {status: 10, value: "已接单"},
                {status: 11, value: "已就餐"},
                {status: 6, value: "交易完成"},
                {status: 7, value: "交易取消"},
                {status: 8, value: "退款中"},
                {status: 9, value: "退款完成"}
            ]
        } else if(type == 4){
            $scope.ordersStatusObj = [
                {status: '', value: "全部订单"},
                {status: 4, value: "已预订"},
                {status: 5, value: "已消费"},
                {status: 6, value: "交易完成"},
                {status: 8, value: "退款中"},
                {status: 9, value: "退款完成"}
            ]
        } else if(type == 5){
            $scope.ordersStatusObj = [
                {status: '', value: "全部订单"},
                {status: 0, value: "未付款"},
                {status: 1, value: "已付款"},
                {status: 10, value: "已接单"},
                {status: 11, value: "可取餐"},
                {status: 6, value: "交易完成"},
                {status: 7, value: "交易取消"},
                {status: 8, value: "退款中"},
                {status: 9, value: "退款完成"}
            ]
        }
        var modalInstance = $modal.open({
            templateUrl: 'tpl/orders/orders_details_export_popup.html',
            controller: 'ordersAndDetailsExportPopupCtrl',
            size: 'sm',
            scope:$scope
        });

        modalInstance.result.then(function (selectedItem) {
            console.log($scope.selected = selectedItem);
            $scope.selected = selectedItem;
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });
    };

    //打开查看服务框
    $scope.checkReserve = function (id) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/orders/reserve_info.html',
            controller: 'reserveInfoCtrl',
            size: 'md',
            scope:$scope,
            resolve: {
                data: function () {
                    return id;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });
    };
}]);

app.controller('reserveInfoCtrl',['$scope','toaster','data','$modalInstance','ordersService',
    function($scope,toaster,data,$modalInstance,ordersService){
    $scope.id=data;
    $scope.ordersInfo={};

    $scope.checkReserve=function(){
        ordersService.checkReserve($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.ordersInfo=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };

    $scope.checkReserve();

    $scope.ok = function(){
        $modalInstance.dismiss('cancel');
    };

}]);

app.controller('ordersViewCtrl',['$scope','ordersService','utilService','$stateParams','toaster',
    function($scope,ordersService,utilService,$stateParams,toaster) {
        $scope.id = $stateParams.id;
        ordersService.findById($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.orders = result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
}]);

//修改订单金额
app.controller('ordersPriceChangeCtrl',['$scope','ordersService','utilService','$stateParams','toaster','$interval','$state',
    function($scope,ordersService,utilService,$stateParams,toaster,$interval,$state) {
    $scope.id = $stateParams.id;
    $scope.changedPayMoney="";
    $scope.priceDifference="";
    ordersService.getOrderById($scope).then(function(result){
        switch (result.status){
            case 200:
                $scope.orders = result.data;
                break;
            default :
                toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                break;
        }

    });


    //更改订单总金额
    $scope.confirmChange=function(){
        ordersService.updateOrdersPrice($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.data = result.data;
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.detail');
                    break;
                case 602:
                    $scope.status=result.status;
                    break;
                case 408:
                    $scope.status=result.status;
                    break;
                case 647:
                    $scope.status=result.status;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };

    $scope.description = "获取验证码";
    $scope.sendSms = function(){
        $scope.canClick=false;
        var second=59;
        var timerHandler;
        ordersService.getMobilePhoneCode($scope).then(function(result){
            switch (result.status){
                case 200:
                    timerHandler = $interval(function(){
                        if(second<=0){
                            $interval.cancel(timerHandler);    //当执行的时间59s,结束时，取消定时器 ，cancle方法取消
                            second=59;
                            $scope.description="获取验证码";
                            $scope.canClick=false;    //因为 ng-disabled属于布尔值，设置按钮可以点击，可点击发送
                        }else{
                            $scope.description=second+"s后重发";
                            second--;
                            $scope.canClick=true;
                        }
                    },1000)   //每一秒执行一次$interval定时器方法
                    break;
                case 680:
                    toaster.pop('error', "操作失败", "不能频繁发送验证码,请稍后再发");
                    break;
                default :
                    alert("发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    //验证手机白名单
    $scope.check = function(){
        if(!angular.isUndefined($scope.orders.userPhone)) {
            ordersService.checkPhone($scope).then(function (result) {
                switch (result.status){
                    case 200:
                        $scope.sendSms();
                        break;
                    case 650:
                        alert("该手机用户无法更改金额");
                        break;
                    default :
                        alert("发生未知错误！请联系管理员");
                        break
                }
            });
        }
    };


}]);


app.controller('ordersGoodsListCtrl',['$scope','ordersService','utilService','$stateParams','$modal','toaster',
    function($scope,ordersService,utilService,$stateParams,$modal,toaster) {
    $scope.id = $stateParams.id;
    $scope.query = function() {
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + 'orders/goods',
            params: {orderId: $scope.id}
        }).then(function (result) {
        });
    };

    $scope.query();

    //查询订单优惠金额
    $scope.orders={};
    $scope.findMinMoney=function(){
        ordersService.findMinMoney($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.orders.couponsMoney=result.data.couponsMoney;
                    $scope.orders.payMoney=result.data.payMoney;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };
    $scope.findMinMoney();

    $scope.changeGoodsStatus=function(goodsId,number){
        var modalInstance = $modal.open({
            templateUrl: 'tpl/orders/refund_info.html',
            controller: 'changeGoodsStatusCtrl',
            size: 'sm',
            scope:$scope,
            resolve: {
                data:function(){
                    var params={
                        orderId:$stateParams.id,
                        goodsId:goodsId,
                        number:number
                    };
                    return params;
                }
            }
        });
    };
}]);

app.controller('changeGoodsStatusCtrl', ['$scope', '$modalInstance','ordersService','toaster','data',
    function($scope, $modalInstance,ordersService,toaster,data) {
        $scope.ordersRefundVO = {
            ordersId:data.orderId,
            ordersGoodsId:data.goodsId,
            maxRefundMoney:0,
            number:data.number
        };
        $scope.isDelModal = true;

        //查询订单商品单价
        $scope.findPrice=function(){
            ordersService.findPrice($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $scope.price=result.data;
                        $scope.money=$scope.price*$scope.ordersRefundVO.number;
                        if($scope.orders.couponsMoney<$scope.orders.payMoney){
                            $scope.ordersRefundVO.maxRefundMoney =
                                $scope.money-$scope.orders.couponsMoney*($scope.money/$scope.orders.payMoney).toFixed(2);
                        }else{
                            $scope.ordersRefundVO.maxRefundMoney = 0;
                        }

                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            })
        };
        $scope.findPrice();

        $scope.ok = function () {
            if (!$scope.isDelModal) {
                return;
            }
            $scope.isDelModal = false;
            ordersService.changeGoodsStatus($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.isDelModal=true;
                        $scope.query();
                        break;
                    case 646:
                        $modalInstance.dismiss('error');
                        toaster.pop('error', "操作失败", "商品不存在");
                        $scope.isDelModal=true;
                        break;
                    case 679:
                        $modalInstance.dismiss('error');
                        toaster.pop('error', "操作失败", "订单商品不存在");
                        $scope.isDelModal=true;
                        break;
                    case 714:
                        $modalInstance.dismiss('error');
                        toaster.pop('error', "操作失败", "退款失败！");
                        $scope.isDelModal=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);

app.controller('updateStatusCtrl', ['$scope', '$modalInstance','ordersService','toaster','modalData',
    function($scope, $modalInstance,ordersService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.order = {
            id: modalData.data.id,
            status:modalData.data.status
        };
        if($scope.order.status == 3){
            $scope.order.confirmType = 3;
        }
        $scope.isDelModal=true;
        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            ordersService.updateStatus($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.isDelModal=true;
                        $scope.query();
                        break;
                    case 615:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.isDelModal=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);


app.controller('deliveryCtrl', ['$scope', '$modalInstance','ordersService','toaster','items',function($scope, $modalInstance,ordersService,toaster,items) {
    $scope.items = items;

    //获得所有物流名称
    $scope.getLogisticsName=function(){
        ordersService.getList($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.logisticsName= result.data;
                    break;
                default :
                    break;
            }
        });
    }

    $scope.getLogisticsName();


    $scope.order = {
        id:$scope.items.id,
        status:$scope.items.status,
        logisticsId:$scope.items.logisticsId,
        logisticsNumber:$scope.items.logisticsNumber
    };
    $scope.ordersService=ordersService;
    $scope.formAllowSubmit=true;

    //添加分类
    $scope.createLogistics=function(){

        if(!$scope.formAllowSubmit){
            return;
        }
        $scope.formAllowSubmit=false;
        ordersService.updateStatus($scope).then(function(result){
            switch (result.status){
                case 200:
                    $modalInstance.dismiss('success');
                    toaster.pop('success', "提示", "操作成功");
                    $scope.query();
                    //$scope.query();
                    $scope.formAllowSubmit=true;
                    break;
                case 606:
                case 607:
                    toaster.pop('waiting', "提示", result.message);
                    $scope.formAllowSubmit=true;
                    break;
                //case 676:
                //    toaster.pop('error', "操作失败", "物流单号错误！");
                //    $scope.formAllowSubmit=true;
                //    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}])
;


app.controller('deliveryUpdateCtrl', ['$scope','ordersService','toaster','$stateParams'
                        ,function($scope,ordersService,toaster,$stateParams) {

    $scope.id = $stateParams.id;

    //获得所有物流名称
    ordersService.getList($scope).then(function(result){
        switch (result.status){
            case 200:
                $scope.logisticsName= result.data;
                break;
            default :
                break;
        }
    });

    ordersService.getOrderById($scope).then(function(result){
        switch (result.status){
            case 200:
                $scope.orders = result.data;
                break;
            default :
                toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                break
        }

    });


    $scope.formAllowSubmit=true;

    $scope.updateLogistics=function(){
        if(!$scope.formAllowSubmit){
            return;
        }
        $scope.formAllowSubmit=false;
        ordersService.updateLogistics($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.query();
                    $scope.formAllowSubmit=true;
                    break;
                case 654:
                    toaster.pop('waiting', "提示", result.message);
                    $scope.formAllowSubmit=true;
                    break;
                //case 676:
                //    toaster.pop('error', "操作失败", "物流单号错误！");
                //    $scope.formAllowSubmit=true;
                //    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }
}]);

//物流追踪模态框Controller
app.controller('logisticsTraceCtrl', ['$scope','ordersService','toaster','data',
    function($scope,ordersService,toaster,data) {
        $scope.logistics=data;

        $scope.logisticsErrorState=false;
        $scope.logisticsTraceState=false;

        $scope.logisticsTrace=function(){
            if($scope.logisticsTraceState){
                return;
            }

            $scope.logisticsTraceState=true;

            ordersService.logisticsTrace($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $scope.logisticsTraceState=false;
                        $scope.data = result.data;
                        break;
                    case 676:
                        $scope.logisticsErrorState=true;
                        break;
                    default :
                        $scope.logisticsTraceState=false;
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }

        $scope.logisticsTrace();

    }
]);

//订单验证码输入模态框
app.controller('ordersCodeCtrl', ['$scope', '$modalInstance','ordersService','toaster','data',
    function($scope, $modalInstance,ordersService,toaster,data) {
        $scope.ordersCode =data;
        $scope.isTrue=true;

        $scope.checkCode = function(){
            if(!$scope.isTrue){
                return;
            }
            $scope.isTrue = false;
            ordersService.checkCode($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        $scope.query();
                        $scope.isTrue=true;
                        break;
                    case 665:
                        $modalInstance.dismiss('error');
                        toaster.pop('warnning', "核销失败", "核销订单未找到！");
                        $scope.isTrue=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);

//导出店铺分析数据模态框Controller
app.controller('ordersAndDetailsExportPopupCtrl', ['$scope', '$modalInstance','ordersService','$filter',
    function($scope, $modalInstance,ordersService,$filter) {
        $scope.AllowToReport=true;
        $scope.ok=function(){
            if(!$scope.AllowToReport){
                return;
            }
            $scope.AllowToReport=false;
            $scope.ordersdetails.start=$filter('date')( $scope.ordersdetails.start,'yyyy-MM-dd');
            $scope.ordersdetails.end=$filter('date')( $scope.ordersdetails.end,'yyyy-MM-dd');
            $scope.ordersdetails.shopName="平台所有商家";
            for (var i = 0; i < $scope.shopSelectList.length; i++) {
                if ($scope.shopSelectList[i].id == $scope.ordersdetails.shopId) {
                    $scope.ordersdetails.shopName = $scope.shopSelectList[i].name;
                    break;
                }
            }
            $scope.ordersdetails.ordersStatus = $scope.ordersdetails.ordersStatusObj.status;
            $scope.ordersdetails.ordersStatusValue = $scope.ordersdetails.ordersStatusObj.value;
            if ($scope.ordersdetails.type == 0) {
                $scope.ordersdetails.ordersExportName = $scope.ordersdetails.start + '至' + $scope.ordersdetails.end + '到店' + $scope.ordersdetails.ordersStatusValue + '订单明细.xls';
            }
            if ($scope.ordersdetails.type == 1) {
                $scope.ordersdetails.ordersExportName = $scope.ordersdetails.start + '至' + $scope.ordersdetails.end + '配送' + $scope.ordersdetails.ordersStatusValue + '订单明细.xls';
            }
            if ($scope.ordersdetails.type == 2) {
                $scope.ordersdetails.ordersExportName = $scope.ordersdetails.start + '至' + $scope.ordersdetails.end + '直邮' + $scope.ordersdetails.ordersStatusValue + '订单明细.xls';
            }
            if ($scope.ordersdetails.type == 3) {
                $scope.ordersdetails.ordersExportName = $scope.ordersdetails.start + '至' + $scope.ordersdetails.end + '堂食' + $scope.ordersdetails.ordersStatusValue + '订单明细.xls';
            }
            if ($scope.ordersdetails.type == 4) {
                $scope.ordersdetails.ordersExportName = $scope.ordersdetails.start + '至' + $scope.ordersdetails.end + '预定' + $scope.ordersdetails.ordersStatusValue + '订单明细.xls';
            }
            if ($scope.ordersdetails.type == 5) {
                $scope.ordersdetails.ordersExportName = $scope.ordersdetails.start + '至' + $scope.ordersdetails.end + '自提' + $scope.ordersdetails.ordersStatusValue + '订单明细.xls';
            }
            ordersService.exportData($scope);
            $modalInstance.dismiss('success');
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);